import { createRouter, createWebHashHistory, createWebHistory, type RouteLocationNormalizedGeneric } from 'vue-router'
import HomeView from '@/views/HomeView.vue';
import { useUserStore } from "@/stores/user"
import { storeToRefs } from 'pinia';
import MyMap from "../views/map/MyMap.vue";
import ArticleList from '../views/article/ArticleList.vue';
import MapList from '../views/map/MapList.vue';
import UpFile from '../views/file/UpFile.vue';
import MapTravel from '../views/map/travel/MapTravel.vue';
import WangEditor from '../views/article/wangEditor/WangEditor.vue';
import AboutView from '../views/about/AboutView.vue';
import Transition from '../views/about/transition/Transition.vue';
import Login from "../views/userInfo/Login.vue"
import Register from '../views/userInfo/Register.vue';
import UserSet from '../views/set/user/UserSet.vue';
import FeatureSet from '../views/set/feature/FeatureSet.vue';
import VisionInfo from '../views/set/Info/VisionInfo.vue';
import FileList from '@/views/file/FileList.vue';
import Set from '@/views/set/Set.vue'
export enum RouterName {
  Home = "Home",
  Set = "Set",
  Login = "Login",
  Register = "Register",
  MyMap = "MyMap",
  MapList = "MapList",
  UpFile = "UpFile",
  FileList = "FileList",
  ArticleList = "ArticleList",
  Travel = "Travel",
  ArticleEdit = "ArticleEdit",
  FeatureSet = "FeatureSet",
  UserSet = "UserSet",
  VisionInfo = "VisionInfo",
}


const router = createRouter({
  // mode: 'history',
  history: createWebHashHistory(),
  // history: createWebHistory(),
  // history: createWebHistory(import.meta.env.BASE_URL),
  routes:[
    {
      name: RouterName.Login,
      path: '/login',
      component: Login,
      meta:{
        canBack:false
      }
    },
    {
      name: RouterName.Register,
      path: '/register',
      component: Register,
      meta:{
        canBack:false
      }
    },
    {
      name: RouterName.Set,
      path: '/set',
      component: Set,
      redirect: { name: "UserSet" },
      children: [
        {
          name: RouterName.FeatureSet,
          path: 'feature_set',
          component: FeatureSet,
          meta :{
            canBack:false
          }
        },
        {
          name: RouterName.UserSet,
          path: 'user_set',
          component: UserSet,
          meta :{
            canBack:false
          }
        },
        {
          path: RouterName.VisionInfo,
          name: 'VisionInfo',
          component: VisionInfo,
          meta :{
            canBack:false
          }
        },
      ]
    }, {
      path: "/",
      component: HomeView,
      redirect: {name:RouterName.ArticleList},
      meta:{
        canBack:true
      },
      children: [
        {
          path: 'article',
          redirect: { name: RouterName.ArticleList },
          children: [
            {
              path: "list",
              name: RouterName.ArticleList,
              component: ArticleList,
              meta :{
                canBack:true
              }
            },
            {
              path: 'edit',
              name: RouterName.ArticleEdit,
              //todo 切换编辑器
              component: WangEditor,
              meta :{
                canBack:true
              }
            }
          ]
        },
        {
          path: "map",
          redirect: { name: RouterName.MyMap },
          children: [
            {
              path: 'mymap',
              name: RouterName.MyMap,
              component: MyMap,
              meta :{
                canBack:true
              }
            }, {
              path: 'maplist',
              name: RouterName.MapList,
              component: MapList,
              meta :{
                canBack:true
              }
            }, {
              path: 'travel',
              name: RouterName.Travel,
              component: MapTravel,
              meta :{
                canBack:true
              }
            },
          ]
        },
        {
          path: "file",
          name:"file",
          redirect: { name: RouterName.FileList },
          children: [
            {
              path: 'upload',
              name: RouterName.UpFile,
              component: UpFile,
              meta :{
                canBack:true
              }
            }, {
              path: 'list',
              name: RouterName.FileList,
              component: FileList,
              meta :{
                canBack:true
              }
            }
          ]
        }
      ]
    }
  ]
})
router.beforeEach(async (to, from, next) => {
  try {
    setLastRoute(from)
    if (to.name === RouterName.Register || to.name === RouterName.Login) {
      if (useUserStore().isLogin === false)
        next()
      else
        next({ path: "/map/articlelist" });
    } else {
      if (useUserStore().isLogin === false) {
        await useUserStore().getUserInfo();
      }
      if (useUserStore().isLogin) {
        if (to.name === "Login" || to.name === "Register") {
          next({ path: "/map/articlelist" });
        } else {
          next();
        }
      } else {
        if (to.name === "Login" || to.name === "Register") {
          next();
        } else {
          // localStorage.setItem("nextRoute", to.name);
          next({ name: "Login" });
        }
      }
    }
  } catch (ex) {
    // localStorage.setItem("nextRoute", to.name);
    console.error(ex);
    next({ name: "Login" });
  }
});

const setLastRoute=(from:RouteLocationNormalizedGeneric)=>{  
  const fromName = from.name as string 
  if(from.meta.canBack){    
    localStorage.setItem("lastRoute",fromName)
  }
}
export const getLastRoute=():string=>{
  const lastRouteName = localStorage.getItem("lastRoute")
  if(lastRouteName)
    return lastRouteName
  else
    return RouterName.ArticleList
}
export default router

